package com.chang.bootplus.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chang.bootplus.base.utils.ApiResponse;
import com.chang.bootplus.base.utils.BeanUtil;
import com.chang.bootplus.entity.UserDepart;
import com.chang.bootplus.mapper.UserDepartMapper;
import com.chang.bootplus.po.UserDepartPO;
import com.chang.bootplus.service.UserDepartService;
import com.chang.bootplus.vo.UserDepartVO;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author chang
 * @since 2022-02-17
 */
@Service
public class UserDepartServiceImp extends ServiceImpl<UserDepartMapper, UserDepart> implements UserDepartService {



    @Override
    public List<UserDepartVO> listDepart() {
        List<UserDepart> allList = list();
        List<UserDepart> list = allList.stream().filter(t -> t.getParentId() == 0).collect(Collectors.toList());
        List<UserDepartVO> userDepartVOS = BeanUtil.listA2ListB(list, UserDepartVO.class);
        userDepartVOS.stream().forEach(t->{
            t.setChildrenList(getChildrenList(t.getId(),allList));
        });
        return userDepartVOS;
    }

    private List<UserDepartVO> getChildrenList(Long parentId,List<UserDepart> allList) {
        List<UserDepart> list = allList.stream().filter(t -> t.getParentId().longValue() == parentId).collect(Collectors.toList());
        List<UserDepartVO> userDepartVOS = BeanUtil.listA2ListB(list, UserDepartVO.class);
        if (userDepartVOS.size()==0){
            return userDepartVOS;
        }
        userDepartVOS.stream().forEach(t->{
            t.setChildrenList(getChildrenList(t.getId(),allList));
        });
        return userDepartVOS;
    }
}
